package com.yxh.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.yxh.R;
import com.yxh.adapter.ViewPageAdapter;
import com.yxh.service.DbSqliteService;
/**
 * index Loading界面
 * 
 * @author luoming
 * 
 */
public class GuideActivity extends BaseActivity {

	private ViewPager mViewPager;
	private int currentItem = 0; // 当前图片的索引号
	private GestureDetector gestureDetector; // 用户滑动
	private int flaggingWidth;// 互动翻页所需滚动的长度是当前屏幕宽度的1/3

	@SuppressLint("InflateParams") @SuppressWarnings("deprecation")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);

		gestureDetector = new GestureDetector(new GuideViewTouch());
		// 获取分辨率
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		flaggingWidth = dm.widthPixels / 3;

		mViewPager = (ViewPager) findViewById(R.id.viewPager);

		LayoutInflater mLi = LayoutInflater.from(this);
		View view1 = mLi.inflate(R.layout.activity_guide_view, null);
		((ImageView) view1.findViewById(R.id.img))
				.setBackgroundResource(R.drawable.app_guide1);

		View view2 = mLi.inflate(R.layout.activity_guide_view, null);
		((ImageView) view2.findViewById(R.id.img))
				.setBackgroundResource(R.drawable.app_guide2);

		View view3 = mLi.inflate(R.layout.activity_guide_view, null);
		((ImageView) view3.findViewById(R.id.img))
				.setBackgroundResource(R.drawable.app_guide3);
		(view3.findViewById(R.id.btn)).setVisibility(View.VISIBLE);

		ArrayList<View> views = new ArrayList<View>();
		views.add(view1);
		views.add(view2);
		views.add(view3);

		PagerAdapter mPagerAdapter = new ViewPageAdapter(views);
		mViewPager.setAdapter(mPagerAdapter);
		mViewPager.addOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageSelected(int position) {
				currentItem = position;

			}

		});

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		if (gestureDetector.onTouchEvent(event)) {
			event.setAction(MotionEvent.ACTION_CANCEL);
		}
		return super.dispatchTouchEvent(event);
	}

	private class GuideViewTouch extends SimpleOnGestureListener {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if (currentItem == 2) {
				if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY()
						- e2.getY())
						&& (e1.getX() - e2.getX() <= (-flaggingWidth) || e1
								.getX() - e2.getX() >= flaggingWidth)) {
					if (e1.getX() - e2.getX() >= flaggingWidth) {
						goToNext();
						return true;
					}
				}
			}
			return false;
		}
	}

	public void startbutton(View v) {
		goToNext();
	}

	private void goToNext() {
		db.modifyConfigItem("isFirst", "1");
		Intent it;
		if ("0".equals(db.getConfigItem(DbSqliteService.USER_ID))) {
			it = new Intent(this, UserLoginActivity.class);
			it.putExtra("source", "1");
		} else {
			it = new Intent(this, MainActivity.class);
		}
		startActivity(it);
		finish();
	}

}
